package org.littlesun.management.ui.login;

import org.eclipse.jface.fieldassist.ControlDecoration;
import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

public class LoginForm extends Dialog {

	private LoginFormController controller;
	protected boolean result = false;
	Shell shell;
	Combo usernameCombo;
	ControlDecoration usernameTextDecorator;
	Text passwordText;
	
	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public LoginForm(Shell parent) {
		super(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		this.controller = new LoginFormController();
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public boolean open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.CLOSE | SWT.TITLE | SWT.SYSTEM_MODAL);
		shell.setSize(369, 193);
		shell.setText("\u767B\u5165");
		shell.setLayout(new FormLayout());

		Label labelPrompt = new Label(shell, SWT.NONE);
		labelPrompt.setFont(SWTResourceManager.getFont("Microsoft JhengHei UI", 12, SWT.NORMAL));
		FormData fd_labelPrompt = new FormData();
		fd_labelPrompt.left = new FormAttachment(0, 10);
		fd_labelPrompt.right = new FormAttachment(100, -10);
		fd_labelPrompt.top = new FormAttachment(0, 10);
		labelPrompt.setLayoutData(fd_labelPrompt);
		labelPrompt.setText("\u8ACB\u8F38\u5165\u4F7F\u7528\u8005\u4EE3\u78BC\u4EE5\u53CA\u5BC6\u78BC");

		Label usernameLabel = new Label(shell, SWT.NONE);
		usernameLabel.setFont(SWTResourceManager.getFont("Microsoft JhengHei UI", 12, SWT.NORMAL));
		FormData fd_usernameLabel = new FormData();
		fd_usernameLabel.left = new FormAttachment(0, 10);
		fd_usernameLabel.top = new FormAttachment(labelPrompt, 17);
		usernameLabel.setLayoutData(fd_usernameLabel);
		usernameLabel.setText("\u4F7F\u7528\u8005\u4EE3\u78BC*");

		this.usernameCombo = new Combo(shell, SWT.BORDER);
		fd_usernameLabel.right = new FormAttachment(usernameCombo, -12);
		usernameCombo.setFont(SWTResourceManager.getFont("Microsoft JhengHei UI", 12, SWT.NORMAL));
		usernameCombo.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				usernameTextDecorator.show();
				System.out.println("usernameTextDecorator.show()");
			}
		});
		FormData fd_text = new FormData();
		fd_text.top = new FormAttachment(labelPrompt, 14);
		fd_text.right = new FormAttachment(100, -29);
		fd_text.left = new FormAttachment(0, 120);
		usernameCombo.setTextLimit(50);
		usernameCombo.setLayoutData(fd_text);

//		new AutoCompleteField(usernameCombo, 
//				ContentProposalProvider.contains(new String[] {"001:AAA", "002:ABB", "003:CAA", "004:XYZ"}), 
//				new ComboContentAdapter());

		Label label_2 = new Label(shell, SWT.NONE);
		label_2.setFont(SWTResourceManager.getFont("Microsoft JhengHei UI", 12, SWT.NORMAL));
		FormData fd_label_2 = new FormData();
		fd_label_2.right = new FormAttachment(usernameLabel, 0, SWT.RIGHT);
		fd_label_2.top = new FormAttachment(usernameLabel, 14);
		fd_label_2.left = new FormAttachment(0, 10);
		label_2.setLayoutData(fd_label_2);
		label_2.setText("\u5BC6\u78BC*");

		passwordText = new Text(shell, SWT.BORDER);
		passwordText.setFont(SWTResourceManager.getFont("Microsoft JhengHei UI", 12, SWT.NORMAL));
		FormData fd_text_1 = new FormData();
		fd_text_1.left = new FormAttachment(usernameCombo, 0, SWT.LEFT);
		fd_text_1.top = new FormAttachment(usernameCombo, 6);
		fd_text_1.right = new FormAttachment(100, -29);
		passwordText.setEchoChar('*');
		passwordText.setTextLimit(30);
		passwordText.setLayoutData(fd_text_1);

		Button button = new Button(shell, SWT.NONE);
		button.setFont(SWTResourceManager.getFont("Microsoft JhengHei UI", 12, SWT.NORMAL));
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (controller.login() == true) {
					LoginForm.this.result = true;
					LoginForm.this.shell.close();
				}
			}
		});
		FormData fd_button = new FormData();
		fd_button.right = new FormAttachment(100, -29);
		fd_button.bottom = new FormAttachment(100, -10);
		button.setLayoutData(fd_button);
		button.setText("\u767B\u5165");

		usernameTextDecorator = new ControlDecoration(usernameCombo, SWT.TOP|SWT.LEFT);
		usernameTextDecorator.setDescriptionText("this is just a test...");

		Image image = FieldDecorationRegistry.
				  getDefault().
				  getFieldDecoration(FieldDecorationRegistry.DEC_INFORMATION).
				  getImage();
		usernameTextDecorator.setImage(image);
		
		Label label = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
		FormData fd_label = new FormData();
		fd_label.top = new FormAttachment(passwordText, 15);
		fd_label.bottom = new FormAttachment(button, -5);
		fd_label.left = new FormAttachment(labelPrompt, 0, SWT.LEFT);
		fd_label.right = new FormAttachment(labelPrompt, 0, SWT.RIGHT);
		label.setLayoutData(fd_label);
		this.controller.bind(this);
	}

	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		LoginForm loginForm = new LoginForm(shell);
		loginForm.open();
	}
}
